﻿#pragma once

#include <QObject>
#include<qstringlist.h>
#include"SongList.h"
#include<qmediaplayer.h>
#include"PlaylistManager.h"
#include<qmediametadata.h>
class QAudioOutput;


class MusicEngine  : public QObject
{
	Q_OBJECT
public:
	enum PlayMode
	{
		PlayInOrder,	//顺序播放
		Shuffle,		//随机播放
		RepeatSingle,	//单曲循环
		//RepeatAll,		//列表循环
		//SingleMode		//单曲播放
	};

private:
	QMediaPlayer* m_player;
	QAudioOutput* m_audioOut;
	PlayMode m_playMode;
	int m_curPlaying;
	PlaylistManager& m_songManager;
	
	
public:
	MusicEngine(QObject *parent);
	~MusicEngine();

	qint64 position() const;
	void setPosition(qint64 pos);
	void play();
	void play(const QUrl& source);
	void play(int listIndex);
	void pause();
	void stop();
	void setPlayMode(MusicEngine::PlayMode mode);
	MusicEngine::PlayMode getCurPlayMode();
	//void setLocalMuseDir(const QString& Dir);
	void setSource(const QUrl& source);
	void setSource(const QString& localPath);
	//点击下一首
	void playNextMusic();
	//点击上一首
	void playPrevMusic();
	QMediaPlayer::PlaybackState playbackState() const;
	QMediaMetaData metaData() const;
	int curDuration();

	QUrl source();
	void setCurIndex(unsigned int index);

public slots:
	void setVolume(int value);


signals:
	void mediaStatusChanged(QMediaPlayer::MediaStatus status);
	void playbackStateChanged(QMediaPlayer::PlaybackState newState);
	void errorOccurred(QMediaPlayer::Error error, const QString& errorString);
	void getCurIndex(int index);
	void positionChanged(qint64 pos);
	void sourceChanged(const QUrl& media);
	void durationChanged(qint64 duration);
private:
	//void musicFromLocal_Async(const QString& dirPath, int batchSize = 50);
	void initAutoPlay();
	void initAllSignal();
};
